package com.example.springtest.user.controller;

import com.example.springtest.common.R;
import com.example.springtest.user.pojo.dto.UserDTO;
import com.example.springtest.user.pojo.entity.User;
import com.example.springtest.user.pojo.vo.UserVO;
import com.example.springtest.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
@Slf4j
@Api(tags="用户")
public class UserController {
@Autowired
    private UserService userService;

    /**
     * 登录
     * @param user
     * @return
     */

@PostMapping("login")
@ApiOperation("登录")
 public R<UserVO> login(@RequestBody User user){
     log.debug("用户登录信息：{}",user);
     UserVO userVO=userService.login(user);
     return R.ok(userVO);
 }

    /**
     * 注册
     * @param userDTO
     * @return
     */

 @PostMapping("register")
 @ApiOperation("注册")
 public R<Void> register(@RequestBody @Validated UserDTO userDTO){
     log.debug("用户注册信息：{}",userDTO);
     userService.register(userDTO);
     return R.ok();
 }

    /**
     * 获取某一个用户
     * @return
     */
 @GetMapping("get/{id}")
 public R<User> getUser(@PathVariable Integer id){
     User user=userService.getUser(id);
     return R.ok(user);
 }

    /**
     * 注销用户
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
 public R<Void> deleteUser(@PathVariable Integer id){
     userService.deleteUser(id);
     return R.ok();
 }

    /**
     * 编辑用户
     * @param id
     * @param user
     * @return
     */
 @PutMapping("edit/{id}")
 public R<Void> editUser(@PathVariable Integer id,@RequestBody @Validated User user){
        userService.editUser(id,user);
        return R.ok();
 }
}
